package com.niit.controller;

import com.niit.entity.Info;
import com.niit.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/info")
public class InfoController {
	@Autowired
	private InfoService infoService;

	@PostMapping("/save")
	public Info saveInfo(@RequestBody Info info) {
		return infoService.save(info);
	}

	@PostMapping("/getAll")
	Page<Info> getAllInfos(
			@RequestParam(required = false) String phone,
			@RequestParam(required = false) String username,
			@RequestParam(required = false) String realName,
			@RequestParam(required = false) String gender,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer size
	){
		Pageable pageable = PageRequest.of(page - 1, size);
		return infoService.findAll(phone, username, realName, gender, pageable);
	}

	@PostMapping("/delete/{id}")
	void deleteInfo(@PathVariable Integer id){
		infoService.deleteById(id);
	}

}
